package com.terabits.smartriver.controller;

import com.terabits.smartriver.constants.result.BaseResp;
import com.terabits.smartriver.constants.result.ResultStatus;
import com.terabits.smartriver.meta.dto.MongoData;
import com.terabits.smartriver.meta.po.Box;
import com.terabits.smartriver.meta.po.BoxMaterial;
import com.terabits.smartriver.meta.po.ProjectMaterial;
import com.terabits.smartriver.service.BoxService;
import com.terabits.smartriver.util.MongoManager;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class BoxController extends BaseController {
    @Resource
    private BoxService boxService;
    @Resource
    private MongoManager mongoManager;

    @ApiOperation(value = "查询 盒子", tags = {"盒子管理"})
    @RequestMapping(value = "/device-mgt/box/get", method = RequestMethod.GET)
    public BaseResp getBox() {
        List box = boxService.getBox(this.getPageData());
        if (box != null && box.size() != 0) {
            return new BaseResp(ResultStatus.SUCCESS, box);

        }
        System.out.println("测试gitee");
        return new BaseResp(ResultStatus.error_record_not_found);
    }

    @ApiOperation(value = "添加 盒子", tags = {"盒子管理"})
    @RequestMapping(value = "/device-mgt/box/add", method = RequestMethod.POST)
    public BaseResp addBox(Box box) {
        int res = boxService.addBox(box);
        if (res == -1) {
            return new BaseResp(ResultStatus.error_duplicated_data);
        }
        return new BaseResp(ResultStatus.SUCCESS, res);
    }

    @ApiOperation(value = "修改 盒子", tags = {"盒子管理"})
    @RequestMapping(value = "/device-mgt/box/update", method = RequestMethod.POST)
    public BaseResp updateBox(Box box) {
        int res = boxService.updateBox(box);
        if (res == -1) {
            return new BaseResp(ResultStatus.error_duplicated_data);
        }
        if (res == -2) {
            return new BaseResp(ResultStatus.error_duplicated_imei);
        }
        return new BaseResp(ResultStatus.SUCCESS, res);
    }

    @ApiOperation(value = "删除 盒子", tags = {"盒子管理"})
    @RequestMapping(value = "/device-mgt/box/delete", method = RequestMethod.POST)
    public BaseResp deleteBox(Integer id) {
        int res = boxService.deleteBox(id);
        if (res == -1) {
            return new BaseResp(ResultStatus.error_delete_failed);
        }
        return new BaseResp(ResultStatus.SUCCESS, res);
    }

    @ApiOperation(value = "下拉 盒子", tags = {"盒子管理"})
    @RequestMapping(value = "/device-mgt/device/box/getSimple", method = RequestMethod.GET)
    public BaseResp getSimpleBox(String projectNum) {
        return new BaseResp(ResultStatus.SUCCESS, boxService.getSimpleBox(projectNum));
    }

    @ApiOperation(value = "添加 盒子资料", tags = {"盒子管理"})
    @RequestMapping(value = "/box-mgt/box/material/add", method = RequestMethod.POST)
    public BaseResp addBoxMaterial(BoxMaterial boxMaterial) {
        boxService.addProjectMaterial(boxMaterial);
        return new BaseResp(ResultStatus.SUCCESS);
    }

    @ApiOperation(value = "查看 盒子资料", tags = {"盒子管理"})
    @RequestMapping(value = "/box-mgt/box/material/get", method = RequestMethod.GET)
    public BaseResp getBoxMaterial(BoxMaterial boxMaterial) {
        return new BaseResp(ResultStatus.SUCCESS, boxService.getProjectMaterial(boxMaterial));
    }

    @ApiOperation(value = "删除 盒子资料", tags = {"盒子管理"})
    @RequestMapping(value = "/box-mgt/box/material/delete", method = RequestMethod.POST)
    public BaseResp deleteBoxMaterial(BoxMaterial boxMaterial) {
        Integer integer = boxService.deleteProjectMaterial(boxMaterial);
        if (integer <= 0) {
            new BaseResp(ResultStatus.error_delete_failed);
        }
        return new BaseResp(ResultStatus.SUCCESS, boxMaterial.getId());
    }

    @ApiOperation(value = "查询读数", tags = {"盒子管理"})
    @RequestMapping(value = "/device-mgt/box/data/get", method = RequestMethod.GET)
    public BaseResp getMongodb(int type, String[] findKeys, String[] findValues, String gt, String lt, String collectionName, String sort) {
        if (type == 1) {
            //某接口的多条数据
            List objects = mongoManager.find(new MongoData(), findKeys, findValues, "createTime", gt, lt, collectionName, "createTime");
            return new BaseResp(ResultStatus.SUCCESS, objects);
        } else if (type == 2) {
            //某接口最后一条数据
            Object one = mongoManager.findOne(new MongoData(), findKeys, findValues, "createTime", gt, lt, collectionName, "createTime", "DESC");
            return new BaseResp(ResultStatus.SUCCESS, one);
        } else if (type == 3) {
            //盒子多条数据
            List objects = mongoManager.find(new MongoData(), "createTime", gt, lt, collectionName, "createTime");
            return new BaseResp(ResultStatus.SUCCESS, objects);
        } else {
            return new BaseResp(ResultStatus.FAIL);
        }
    }

}
